from guiGraph import *
import graph.graph
import graph.operations
import analyses.topology.properties


t_graphObj = None
def analyze(command):
    weighted = False
    directed = False
    
    command.remove('analyze')
    if '-w' in command:
        command.remove('-w')
        weighted = True
    if '-d' in command:
        command.remove('-d')
        directed = True
    if len(command) < 1:
        print "Cannot determine input file name."
        return
    ip = open(command.pop(0), 'r')
    edgelist = ip.read().split("\n")
    ip.close()
    #g = graph.graph.graph(directed, weighted)
    #g = guiGraph(directed, weighted)
    #g.create(edgelist)
    if len(command) < 1:
        print "here" , edgelist
	global t_graphObj
	t_graphObj = graph.graph.graph(False, False)
	#edgelist2 = ['1 2', '2 3', '3 4', '4 1']
	t_graphObj.create(edgelist)
        analyses.topology.properties.computeProperties(t_graphObj)
        #analyses.topology.properties.computeProperties(g)
    else:
        print "there"
        analyses.topology.properties.computeProperties(g, command.pop(0))
    g.reset()
    return
    
def start():
    print "Welcome to TopAZ."
    print "The program prompt is 'topaz>' at which you can type commands."
    print "Type 'help' for help regarding the commands. Type 'info' for information about the program. Type 'quit' to end the program."
    print "To analyze a graph, type 'analyze <path/of/input/file> [-d] [-w] [<path/of/output/file>]"
    print "Check the 'readme' for input file format."
    print "TopAZ assumes unweighted, undirected graphs by default. Use '-d' if the graph is directed, and '-w' if it is weighted."
    while True:
        command = raw_input("topaz> ")
        command = command.split()
        if command[0] == 'help':
            print "To analyze a graph, type 'analyze <path/of/input/file> [-d] [-w] [<path/of/output/file>]"
            print "Check the 'readme' for input file format. TopAZ takes a list of edges with or without weights as input."
            print "TopAZ assumes unweighted, undirected graphs by default. Use '-d' if the graph is directed, and '-w' if it is weighted."
            print "If you do not specify an output file, output will be appended to the default file 'op.txt' in TopAZ root."
        elif command[0] == 'info':
            print "TopAZ python version was developed by Sanket Patil."
        elif command[0] == 'analyze':
            analyze(command)    
        elif command[0] == 'quit':
            print "Thanks for using TopAZ!"
            break
        else:
            print "Invalid command. Please use 'help' for command syntax."
            break

if __name__ == "__main__":
    start()
